/*
 * CanvaListItem.java
 *
 * Created on 4 de Junho de 2007, 20:51
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.view.components;

import java.util.Random;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author $Leandro$
 */
public class CanvasListItem extends CanvasAbstract implements Runnable{
    
    public Image imgIcon;
    
    public Image imgIconSelected;
    
    public String text;
    
    public int id;
    
    public CanvasColor bgColor;
    
    public CanvasColor textColor;
    
    public int align;
    
    public Font font = Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
    
    public boolean isSelected = false;
    
    private int delta = 2;
    
    Thread t;
    
    public CanvasListItem(Graphics g,String text, int align,CanvasColor bgColor, CanvasColor textColor) {
        super(g);
        this.bgColor = bgColor;
        this.textColor = textColor;
        this.text = text;
        this.align = align;
        Thread t = new Thread(this);
    }
    
    public void printIcon() {
        if(isSelected) {
            if(imgIconSelected != null) {
                g.drawImage(imgIconSelected,x,y,align);
                x += imgIconSelected.getWidth();
            }
        } else {
            if(imgIcon != null) {
                g.drawImage(imgIcon,x,y,align);
                x += imgIcon.getWidth();
            }
        }
    }
    
    public void paint() {
        g.setFont(font);
        g.setColor(textColor.rgb);
        printIcon();
        g.drawString(text,x,y,align);
    }
    
    public int getImgWidth() {
        int tamIcon = 0 , tamSel = 0;
        
        if(imgIcon != null) {
            tamIcon = imgIcon.getWidth();
        }
        
        if(imgIconSelected != null) {
            tamSel = imgIconSelected.getWidth();
        }
        
        if(tamIcon > tamSel) {
            return tamIcon;
        } else {
            return tamSel;
        }
    }
    
    public int getImgHeight() {
        int tamIcon = 0 , tamSel = 0;
        
        if(imgIcon != null) {
            tamIcon = imgIcon.getHeight();
        }
        
        if(imgIconSelected != null) {
            tamSel = imgIconSelected.getHeight();
        }
        
        if(tamIcon > tamSel) {
            return tamIcon;
        } else {
            return tamSel;
        }
    }
    
    private void drawImgWithAlpha(Image img) {
        int[] rgbData = new int[img.getWidth()*img.getHeight()];
        img.getRGB(rgbData, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());
        g.drawRGB(rgbData, 0, img.getWidth(), x, y, img.getWidth(), img.getHeight(), true);
    }
    
    public void run() {
        while(true) {
            g.drawImage(imgIcon,x + new Random(System.currentTimeMillis()).nextInt(3),y,align);
            try {
                t.sleep(100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
}